package com.bdqn.wang.servlet;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.db.Page;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bdqn.wang.pojo.*;
import com.bdqn.wang.service.ComdityAddService;
import com.bdqn.wang.service.ComdityAdmService;
import com.bdqn.wang.service.ComdityService;
import com.bdqn.wang.service.impl.ComdityAddServiceImpl;
import com.bdqn.wang.service.impl.ComdityAdmServiceImpl;
import com.bdqn.wang.service.impl.ComdityServiceImpl;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.management.openmbean.CompositeDataSupport;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@WebServlet(urlPatterns = {"/comodity"})
public class ComdityServlet extends HttpServlet {

    ComdityService comdityService = new ComdityServiceImpl();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        String opr = req.getParameter("opr");
        if ("tolist".equals(opr)){
            req.getRequestDispatcher("/WEB-INF/comodity/commodity.jsp").forward(req,resp);
        }else if ("list".equals(opr)){
            List<Comdity> comdityList = comdityService.getAll();
            req.getSession().setAttribute("list",comdityList);
            resp.getWriter().print(JSON.toJSONString(comdityList));
        } else if("del".equals(opr)){
            String id = req.getParameter("id");
            int count = comdityService.del(Integer.parseInt(id));
            if(count>0){
                req.getRequestDispatcher("/WEB-INF/comodity/commodity.jsp").forward(req,resp);
            } else {
                resp.getWriter().print("<script>alert('删除失败')</script>");
            }
        }else if ("toadd".equals(opr)){
            req.getRequestDispatcher("/WEB-INF/comodity/commodity_added.jsp").forward(req,resp);
        }else if("added".equals(opr)){
            Comdity comdity = new Comdity();
            comdity.setName(req.getParameter("name"));
            comdity.setParentId(Integer.parseInt(req.getParameter("parentId")));
            comdity.setType(Integer.parseInt(req.getParameter("type")));
            comdity.setIconClass(req.getParameter("iconClass"));
            int count = comdityService.add(comdity);
            if(count > 0){
                req.getRequestDispatcher("/WEB-INF/comodity/commodity.jsp").forward(req, resp);
            }else {
                req.setAttribute("msg","新增商品分类信息失败！");
            }
        }
    }
}
